package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.Wish;
import org.csu.mypetstore.persistence.AccountMapper;
import org.csu.mypetstore.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountService {

  @Autowired
  private AccountMapper accountMapper;

  public Account getAccount(String username) {
    return accountMapper.getAccountByUsername(username);
  }

  public Account getAccount(String username, String password) {
    Account account = new Account();
    account.setUsername(username);
    account.setPassword(password);
    return accountMapper.getAccountByUsernameAndPassword(account);
  }

  public void insertAccount(Account account) {
    accountMapper.insertAccount(account);
    accountMapper.insertProfile(account);
    accountMapper.insertSignon(account);
  }

  public void updateAccount(Account account) {
    accountMapper.updateAccount(account);
    accountMapper.updateProfile(account);

    if (account.getPassword() != null && account.getPassword().length() > 0) {
      accountMapper.updateSignon(account);
    }
  }
  public List<Account> getallAccount(){
    return accountMapper.getallAccount();
  }

  public List<Account> getallAccountprofile(){
    return accountMapper.getallAccountprofile();
  }

  public List<Account> searchAccount(String keyword){
    return accountMapper.searchAccount(keyword);
  }

  public List<Account> searchAccountprofile(String keyword){
    return accountMapper.searchAccountprofile(keyword);
  }

  public void deleteAccount(String username){
    accountMapper.deleteAccount(username);
    accountMapper.deleteprofile(username);
    accountMapper.deleteSignon(username);
  }

  public void insertWish(Wish wish){
    accountMapper.insertWish(wish);
  }

  public void deleteWish(Wish wish){
    accountMapper.deleteWish(wish);
  }

  public List<Wish> selectWish(String username){
    return accountMapper.selectWish(username);
  }
  public String getAccountbyphone(String phonenumber){
    return accountMapper.getAccountnamebyphone(phonenumber);
  }

}
